<template>
  <div class="app-container">
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="作者">
        <template slot-scope="{row}">
          <ul class="list-unstyled">
            <li>
              <a :href="row.site" target="_blank">
                <img style="width: 20px;border-radius: 50%;position: relative;top: -2px;" :src="row.avatar" />{{ row.nickname }}
              </a>
            </li>
            <li><span style="color: #a9a9a9;">{{ row.ip }}</span></li>
            <li><span style="color: #a9a9a9;">{{ row.province }}{{ row.city }}</span></li>
            <li><span style="color: #a9a9a9;">{{ row.createTime }}</span></li>
          </ul>
        </template>
      </el-table-column>
      <el-table-column label="内容">
        <template slot-scope="{row}">
          <div style="border-bottom: 1px solid #dcddde;margin-bottom: 10px;">
            <span>评论自：</span>
            <a v-if="row.postId === 0" :href="SiteSettings.siteWebUrl + '/guestbook'" target="_blank">
              <strong>留言板</strong>
            </a>
            <a v-else-if="row.postId === -1" :href="SiteSettings.siteWebUrl + '/links'" target="_blank">
              <strong>友情链接</strong>
            </a>
            <a v-else :href="SiteSettings.siteWebUrl + '/article/' + row.article.id" target="_blank">
              <strong>{{ row.article.title }}</strong>
            </a>
          </div>
          <div>
            {{ row.content }}
            <div v-if="row.parent" style="background-color: #f1f1f1;padding: 5px;margin: 5px;border-radius: 4px;">
              <div style="padding-left: 10px;">
                <i class="fa fa-quote-left fa-fw"/>
                <strong>原评：</strong>
                <span>{{ row.parent.content }}</span>
              </div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="赞" prop="support" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.support }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="{row}">
          <el-switch @change="updateStatus(row)" v-model="row.status"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            <i class="fa fa-trash-o fa-lg" />
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>
<script>
import { feachList, updateStatus, del } from '@/api/comment'
import Pagination from '@/components/Pagination'
import { mapGetters } from 'vuex'
import { MessageBox } from 'element-ui'
export default {
  name: 'Tag',
  components: {
    Pagination
  },
  data() {
    return {
      listLoading: false,
      tableKey: 0,
      total: 0,
      list: [],
      listQuery: {
        page: 1,
        limit: 10,
        status: null
      }
    }
  },
  computed: {
    ...mapGetters([
      'SiteSettings'
    ])
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { result } = await feachList(this.listQuery)
      this.listLoading = false
      this.list = result.list
      this.total = result.total
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    message(title, type, message) {
      this.$notify({
        title: title,
        message: message,
        type: type,
        duration: 2000
      })
    },
    updateStatus(row) {
      updateStatus(row.id).then((res) => {
        this.message('状态更改', res.message, 'success')
      })
    },
    handleDelete(row) {
      MessageBox.confirm(`是否确认删除【${row.content}】这条数据？`, '删除', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.id).then(res => {
          if (res.code === 0) {
            this.message('删除评论', 'success', res.message)
            this.handleFilter()
          }
        })
      })
    }
  }
}
</script>
<style>
  .list-unstyled {
    padding-left: 0;
    list-style: none;
  }
</style>
